package com.itheima.reggie.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Page;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.entity.dto.SetmealDto;
import com.itheima.reggie.exception.CustomerException;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    @Autowired(required = false)
    private SetmealDishMapper setmealDishMapper;

    @Autowired(required = false)
    private CategoryMapper categoryMapper;

    @Override
    @Transactional
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void save(SetmealDto setmealDto) {
        //1. 补全setmeal的信息，比如:、修改、创建时间、
        setmealDto.setCreateTime(LocalDateTime.now());
        setmealDto.setUpdateTime(LocalDateTime.now());
        setmealDto.setStatus(1);

        //2. 调用保存方法
        setmealMapper.save(setmealDto);

        //3. 得到套餐的菜品，给所有的菜品补全信息(setmeal_id 、修改、创建时间、排序，)
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes().stream().map(setmealDish -> {
            setmealDish.setSetmealId(setmealDto.getId());
            setmealDish.setCreateUser(setmealDto.getCreateUser());
            setmealDish.setUpdateUser(setmealDto.getUpdateUser());
            setmealDish.setCreateTime(LocalDateTime.now());
            setmealDish.setUpdateTime(LocalDateTime.now());
            setmealDish.setSort(0);
            return setmealDish;
        }).collect(Collectors.toList());

        //4. 批量导入套餐菜品
        setmealDishMapper.saveBatch(setmealDishes);
    }

    @Override
    public R<Page<SetmealDto>> findByPage(Integer page, Integer pageSize, String name) {
        //1.设置当前页和页面大小
        PageHelper.startPage(page,pageSize);

        //2.根据套餐名字搜索套餐
        List<Setmeal> setmealList = setmealMapper.findByName(name);

        //3.得到PageInfo对象
        PageInfo<Setmeal> pageInfo = new PageInfo<>(setmealList);

        //4.得到所有的Setmeal，把Setmeal转换为SetmealDto,并且设置套餐的名字
        List<SetmealDto> setmealDtoList = setmealList.stream().map(setmeal -> {
            //属性拷贝
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(setmeal, setmealDto);
            //设置类别的名字
            Category category = categoryMapper.findById(setmeal.getCategoryId());
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        //5.构建page对象
        Page<SetmealDto> pageResult = new Page<>(setmealDtoList,pageInfo.getTotal(),page,pageSize);
        return R.success(pageResult);
    }


    @Override
    @CacheEvict(value = "setmealCache",allEntries = true)
    public void delectByIds(List<Long> ids) {
       //1.删除之前先检查套餐是否在售，如果在售不允许删除
       long count = setmealMapper.findSetmealByStatus(ids);
       if (count>0){
           throw new CustomerException("不允许删除在售的套餐");
       }

       //2.如果套餐不是在售状态则删除套餐
        setmealMapper.deleteByIds(ids);

       //3.删除套餐关联菜品
        setmealDishMapper.deleteBysetmealIds(ids);
    }

    @Override
    public void updateStatusById(List<Long> ids, Integer status) {
        setmealMapper.updateStatusById(ids,status);
    }

    @Override
    @Cacheable(value = "setmealCache",key = "#categoryId+'_'+#status")
    public List<Setmeal> list(Long categoryId, Integer status) {
        List<Setmeal> setmealList = setmealDishMapper.list(categoryId,status);
        return setmealList;
    }


}
